package com.unicom.priceboard.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import com.unicom.priceboard.data.vo.CheckListResult;
import com.unicom.priceboard.data.vo.HaIndex;
import com.unicom.priceboard.data.vo.HaStock;
import com.unicom.priceboard.data.vo.HoIndex;
import com.unicom.priceboard.data.vo.HoStock;
import com.unicom.priceboard.data.vo.PutAdElement;
import com.unicom.priceboard.data.vo.PutExecElement;

/**
 * @author quan
 *
 */
public class JSonConvert {
	private static final Log logger = LogFactory.getLog(JSonConvert.class);
	
	/**
	 * @param jsonString
	 * @return object of HoIndex class from the given JSON string
	 */
	public static HoIndex hoIndexFromJson(String jsonString) {
		Gson gSON = new Gson();
		return gSON.fromJson(jsonString, HoIndex.class);
	}
	
	/**
	 * @param jsonString
	 * @return object of putAd class from the given JSON string
	 */
	public static PutAdElement PutAdFromJson(String jsonString) {
		Gson gSON = new Gson();
		return gSON.fromJson(jsonString, PutAdElement.class);
	}
	/**
	 * @param jsonString
	 * @return object of HaIndex class from the given JSON string
	 */
	public static HaIndex haIndexFromJson(String jsonString) {
		Gson gSON = new Gson();
		return gSON.fromJson(jsonString, HaIndex.class);
	}
	
	/**
	 * @param oIndex (HASTC Index object)
	 * @return JSON object for HASTC Index
	 */
	public static String converToJSON(HaIndex oIndex) {
		Gson gSON = new Gson();
		return gSON.toJson(oIndex, HaIndex.class);
	}
	
	/**
	 * @author quan
	 * @param oStock
	 * @return JSON object for HASTC stock
	 */
	public static String convertToJSON(HaStock oStock) {
		Gson gSon = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
		String retVal = "";
		
		try{
			retVal = gSon.toJson(oStock, HaStock.class);
		}catch(JsonParseException jse) {
			logger.error("error in convering HASTC stock "+oStock.getSymbol()+" to JSON string");
		}
		return retVal;
	}
	
	/**
	 * @param oCheck
	 * @return JSON object for checkResult
	 */
	public static String convertToJSON(CheckListResult oCheck) {
		Gson gSON = new Gson();
		return gSON.toJson(oCheck,CheckListResult.class);
	}
	
	/**
	 * @author Nga 
	 * @param oStock
	 * @return JSON object for put through deals stock
	 */
	public static String convertToJSON(PutAdElement oStock) {
		Gson gSon = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
		String retVal = "";
		
		try{
			retVal = gSon.toJson(oStock, PutAdElement.class);
		}catch(JsonParseException jse) {
			logger.error("error in convering HASTC stock "+oStock.getSymbol_code()+" to JSON string");
		}
		return retVal;
	}
	/**
	 * @author Nga 
	 * @param oStock
	 * @return JSON object for put through deals stock
	 */
	public static String convertToJSON(PutExecElement oStock) {
		Gson gSon = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
		String retVal = "";
		
		try{
			retVal = gSon.toJson(oStock, PutExecElement.class);
		}catch(JsonParseException jse) {
			logger.error("error in convering HASTC stock "+oStock.getSymbolStock()+" to JSON string");
		}
		return retVal;
	}
	
	/**
	 * @param oIndex (HOSE Index object)
	 * @return JSON object for HOSE Index
	 */
	public static String convertToJSON(HoIndex oIndex) {
		Gson gSon = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
		return gSon.toJson(oIndex, HoIndex.class);
	}
	
	/**
	 * @author quan
	 * @param oStock
	 * @return JSON object for HOSE stock
	 */
	public static String convertToJSON(HoStock oStock) {
		Gson gSon = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
		String retVal = "";
		
		try{
			retVal = gSon.toJson(oStock, HoStock.class);
		}catch(JsonParseException jse) {
			logger.error("error in convering HASTC stock "+oStock.getSymbol_code()+" to JSON string");
		}
		return retVal;
	}
}
